package cn.satoken.controller;

import cn.dev33.satoken.util.SaResult;
import cn.satoken.entity.CrmCustomer;
import cn.satoken.service.CustomerService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <h3>saToken</h3>
 * <p></p>
 *
 * @author gdw
 * @since 2025-06-05 09:40:18
 */
@RestController
@RequestMapping("/customer/")
@RequiredArgsConstructor
public class CustomerController {
    private final CustomerService customerService;

    /**
     * 保存或者修改方法
     * @param customer
     * @return
     */
    @PostMapping("save")
    public SaResult saveCustomer(@RequestBody CrmCustomer customer){
    if(customer.getId()==null){
        return customerService.saveCustomer(customer);
    }else{
        return customerService.updateCustomer(customer);
    }
    }

    /**
     * 分页查询
     */
    @GetMapping("page")
    public SaResult selectPage(@RequestParam int page, @RequestParam int size,@RequestParam Map<String, Object> map) {
        if(page <= 0 || size <= 0) {
            page = 1;
            size = 10;
        }
        Map<String, Object> condition = new HashMap<>();
        condition.put("name", map.get("name") != null ? map.get("name").toString() : null);
        condition.put("source", map.get("source") != null ? map.get("source").toString() : null);
        condition.put("startTime", map.get("startTime") != null ? map.get("startTime").toString() : null);
        condition.put("endTime", map.get("endTime") != null ? map.get("endTime").toString() : null);
        return customerService.selectPage(page,size,condition);
    }
}
